<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;
class FinancialController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//代理商入账消费
    public function index(){
        $type = I('type');//1入账2消费
        if(!$type){
            $type = 1;
        }
        $search = I('search');
        if($search){
            $where['a.user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $where['c.type'] = $type;
        $this->assign("type", $type);
        $header = C('header')['charge'];
        $count =  M('charge')
                    ->alias("c")
                    ->join("left join pt_admin as a on a.admin_id=c.admin_id")
                    ->field('c.*,a.user_name')
                    ->where($where)
                    ->count();
        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $list = M('charge')
                ->alias("c")
                ->join("left join pt_admin as a on a.admin_id=c.admin_id")
                ->field('c.admin_id,c.charge_id,a.user_name,c.amount,c.logtime')
                ->where($where)
                ->order('charge_id desc')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
            $list[$k]['user_name'] = "<a href='/Admin/Financial/customer?admin_id={$v['admin_id']}&type={$type}'>{$v['user_name']}</a>";
            unset($list[$k]['admin_id']);
        }
        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Financial');
        $this->display();
    }



    //用户入账消费
    public function customer(){
        $admin_id = I('admin_id');
        $type = I('type');
        if(!$admin_id || !$type){
            $this->error('请填写完整信息');
        }
		$search = I('search');
        if($search){
            $where['a.user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $where['c.admin_id'] = $admin_id;
        $where['c.type'] = $type;
        $header = C('header')['customer'];
        $count =  M('ucharge')
                    ->alias("c")
                    ->join("left join pt_user as a on a.user_id=c.uid")
                    ->field('c.*,a.user_name')
                    ->where($where)
                    ->count();
        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $list = M('ucharge')
                ->alias("c")
                ->join("left join pt_user as a on a.user_id=c.uid")
                ->field('c.charge_id,a.user_name,c.amount,c.logtime')
                ->where($where)
                ->order('charge_id desc')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
        }
        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Financial');
        $this->display();
    }

    



    public function charge(){
        if(IS_POST){
            $request = I('post.');
            if(empty($request['phone']) || empty($request['user_name']) || empty($request['amount']) || empty($request['websites'])){
                $this->error('请填写完整信息');
            }
            $where['phone'] = $request['phone'];
            $where['user_name'] = $request['user_name'];
            $where['websites'] = $request['websites'];
            $list = M('admin')->where($where)->find();
            if(empty($list)){
                $this->error('代理商信息错误或不存在');
            }

            $res['uid'] =  $list['user_id'];
            $res['admin_id'] =  $list['admin_id'];
            $res['amount'] =   $request['amount'];
            $res['logtime'] =  time();
            $res['type'] = 1;

            $data = M('charge')->add($res);

            $up['money'] = $list['money']  + $request['amount'];
            $up['status'] = 0;
            $st = M('admin')->where($where)->save($up);
            if(empty($data) || empty($st)){
                $this->error('充值失败');
            }else{
                $this->success("充值成功", U("Financial/charge"));exit;
            }
        }
        $this->assign("navs", 'Financial');
        $this->display();
    }




    
}